package com.example.tilitili.vo;

import com.example.tilitili.utils.ValidatorUtil;
import com.example.tilitili.validator.IsMobile;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
 * @program: utilitili-member-purchase
 * @description: 校验信息注解规则
 * @author: chenyu
 * @create: 2022-01-13 13:46
 **/

public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {/*泛型：注解 注解定义*/
    private boolean required = false;
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();/*初始化：是否必填*/
    }
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (required)
        {/*必填*/
            return ValidatorUtil.isMobile(value);
        }
        else
        {/*非必填*/
            if (StringUtils.isEmpty(value))
            {
                return true;
            }
            else
            {
                return ValidatorUtil.isMobile(value);
            }
        }
    }
}